package com.itheima.exceptions;

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.fasterxml.jackson.core.io.JsonEOFException;
import com.itheima.enums.MsgCode;

import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;

/**
 * @Description: 异常扑捉类
 * @Date :2019-08-17 10:36
 * @Author : yuan_he_song
 */
@RestControllerAdvice
public class ExceptionHandlerController {

    /**
     *  捕获自定义异常
     * @param customException
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public Map<String,Object> catchException(CustomException customException){
        Map<String, Object> result = new HashMap<>();
        result.put("code",customException.getCode());
        result.put("msg",customException.getMsg());
        return result;
    }

    /**
     *  捕获token过期异常
     */
    @ExceptionHandler(ExpiredJwtException.class)
    public Map<String,Object> catchExpiredJwtException(){
        Map<String, Object> result = new HashMap<>();
        result.put("code", MsgCode.ERROR_LOGIN.getCode());
        result.put("msg",MsgCode.ERROR_LOGIN.getMsg());
        return result;
    }
    /**
     *  token被篡改异常
     */
    @ExceptionHandler({JsonEOFException.class,SignatureException.class})
    public Map<String,Object> catchJsonEOFException(){
        Map<String, Object> result = new HashMap<>();
        result.put("code", MsgCode.TOKEN_CHANGE.getCode());
        result.put("msg",MsgCode.TOKEN_CHANGE.getMsg());
        return result;
    }
}
